/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Controlador.Bean.Ventas.PedidoBusqueda;
import Controlador.Bean.Ventas.LineaCotizacion;
import Controlador.Service.Ventas.GestionVentas;
import Controlador.Bean.Ventas.Facturacion;
import Controlador.Bean.Ventas.NotaCreditoBusqueda;
import Controlador.Service.Configuracion.GestionConfiguracion;
import javax.swing.JDialog;
import javax.swing.table.AbstractTableModel;
import java.util.Date;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Luis Enrique
 */
public class FacturacionRegistrarPanel extends javax.swing.JPanel {

    PedidoTableModel myTableModel;
    NotaCreditoBusqueda ncb = new NotaCreditoBusqueda();
    private JDialog parent;
    private FacturacionMainPanel refPanel;
    Date fecha = new Date ();
    PedidoBusqueda pedido = new PedidoBusqueda();
    ArrayList<LineaCotizacion> lineas = new ArrayList<>();
    private double igv = GestionConfiguracion.getInstance().getIGV();
    private double tipoCambio = GestionConfiguracion.getInstance().getTipoCambio();
    //Formato decimal
    DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
    DecimalFormat df;
    
    DecimalFormat formateador;
    
    public void setParent(JDialog parent){
        this.parent = parent;
    }
    
    public void setRefPanel(FacturacionMainPanel panel){
        this.refPanel = panel;
    }
            
    class PedidoTableModel extends AbstractTableModel{
        String [] titles = {"Linea de Documento", "Producto","Precio Unitario",
                            "Cantidad","SubTotal"};

        @Override
        public int getRowCount() {
            return lineas.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            LineaCotizacion lc = lineas.get(rowIndex);
            switch(columnIndex){
                case 0: res = lc.getNumLinea()+""; break;
                case 1: res = lc.getNombre();break;
                case 2: res = lc.getPrecioUnitario()+""; break;
                case 3: res = lc.getCantidad()+"";break;
                case 4: res = df.format(lc.getSubTotal()); break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public FacturacionRegistrarPanel() {
        initComponents();
        btnGpMoneda.add(rbDolares);
        btnGpMoneda.add(rbSoles);
        btnGpTipo.add(rbBoleta);
        btnGpTipo.add(rbFactura);
        
        txtNumPedido.setEnabled(false);
        dpFechaPed.setEnabled(false);
        txtNumCotizacion.setEnabled(false);
        dpFechaCot.setEnabled(false);
        txtNombre.setEnabled(false);
        txtNumDoc.setEnabled(false);
        dpFechaVenta.setEnabled(false);
        dpFechaVenta.setDate(fecha);
        txtPorcentajeIGV.setEnabled(false);
        txtImporteIGV.setEnabled(false);
        txtMontoSinIGV.setEnabled(false);
        txtFinalSoles.setEnabled(false);
        txtFinalDolares.setEnabled(false);
        txtTipoCambio.setEnabled(false);
        txtMontoNC.setEnabled(false);
        txtMontoFinalFinalSoles.setEnabled(false);
        this.simbolos.setDecimalSeparator('.');
        this.df = new DecimalFormat("#.00",simbolos);
        this.formateador= new DecimalFormat("###.##%",simbolos);
        btnSeleccionarNC.setEnabled(false);
        
        myTableModel = new PedidoTableModel();
        tblProductos.setModel(myTableModel);
        TableRowSorter<PedidoTableModel> sorter = new TableRowSorter<> ((PedidoTableModel) tblProductos.getModel());
        tblProductos.setRowSorter(sorter);
    }

    public void refreshItemsTable(){
        myTableModel.fireTableChanged(null);        
    }
    
    public void llenaPedido(){
        lineas = GestionVentas.getInstance().consultarLineasCotizacion(pedido.getIdCotizacion());
        refreshItemsTable();
        double montofinalsoles = Double.parseDouble(pedido.getTotal()+"");
        txtNumPedido.setText(pedido.getNumeroPed()+"");
        dpFechaPed.setDate(pedido.getFechaPed());
        txtNumCotizacion.setText(pedido.getNumeroCot()+"");
        dpFechaCot.setDate(pedido.getFecha());
        txtNombre.setText((pedido.getRazonSocial()!=null)?pedido.getRazonSocial():pedido.getApePaterno()+" "+pedido.getApeMaterno()+", "+pedido.getNombres());
        txtNumDoc.setText(pedido.getNumDoc()+"");
        rbSoles.setSelected(true);
        txtPorcentajeIGV.setText(formateador.format(igv));
        txtImporteIGV.setText(df.format(GestionVentas.getInstance().Redondear(igv*montofinalsoles/(1+igv)))+"");
        txtMontoSinIGV.setText(df.format(GestionVentas.getInstance().Redondear(montofinalsoles/(1+igv)))+"");
        txtFinalSoles.setText(df.format(pedido.getTotal())+"");
        txtMontoNC.setText("-");
        txtMontoFinalFinalSoles.setText("-");
        txtTipoCambio.setText("-");
        txtFinalDolares.setText("-");
    }
    
    public boolean valido(){
        boolean b = true;
        if(!rbBoleta.isSelected() && !rbFactura.isSelected()){
            b = false;
        }
        return b;
    }
                
    public void llenaNC(){
        txtMontoNC.setText(df.format(ncb.getMonto()));
        double montoFinalSoles1 = Double.parseDouble(txtFinalSoles.getText()+"");
        double montoNC = Double.parseDouble(txtMontoNC.getText()+"");
        if(montoNC>montoFinalSoles1)
            txtMontoFinalFinalSoles.setText("0.00");
        else
            txtMontoFinalFinalSoles.setText(df.format(pedido.getTotal()-ncb.getMonto())+"");
    }
    
    public void actualizaDolares(){
        double monto;
        if(rbDolares.isSelected()){
            if(txtMontoFinalFinalSoles.getText().equals("-")){
                monto = Double.parseDouble(pedido.getTotal()+"");
                txtTipoCambio.setText(this.tipoCambio+"");
                txtFinalDolares.setText(df.format(GestionVentas.getInstance().Redondear(monto*tipoCambio)));
            }
            else{
                monto = Double.parseDouble(txtMontoFinalFinalSoles.getText()+"");
                txtTipoCambio.setText(this.tipoCambio+"");
                if(monto==0)
                    txtFinalDolares.setText("0.00");
                else
                    txtFinalDolares.setText(df.format(GestionVentas.getInstance().Redondear(monto*tipoCambio)));
            }
        }
    }
    
    public boolean stockValido(){
        for(int i=0;i<lineas.size();i++){
            if(Controlador.Service.Almacen.GestionAlmacenes.obtenerStock(lineas.get(i).getIdProducto())<lineas.get(i).getCantidad())
                return false;                
        }
        return true;
    }
    
    public void registrarSalidas(){
        GestionVentas.GenerarSolicitudMovimientoLC(lineas);
    }
    
    public void habilita(){
        btnSeleccionarNC.setVisible(true);
        btnSeleccionarNC.setEnabled(true);
        btnRegistrar.setEnabled(true);
        btnRegistrar.setVisible(true);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGpMoneda = new javax.swing.ButtonGroup();
        btnGpTipo = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        txtNumPedido = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        dpFechaPed = new org.jdesktop.swingx.JXDatePicker();
        jLabel5 = new javax.swing.JLabel();
        txtNumCotizacion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        dpFechaCot = new org.jdesktop.swingx.JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        btnPedido = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        dpFechaVenta = new org.jdesktop.swingx.JXDatePicker();
        jPanel2 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        rbBoleta = new javax.swing.JRadioButton();
        rbFactura = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        rbSoles = new javax.swing.JRadioButton();
        rbDolares = new javax.swing.JRadioButton();
        jLabel11 = new javax.swing.JLabel();
        txtPorcentajeIGV = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtImporteIGV = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtMontoSinIGV = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtFinalSoles = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtMontoNC = new javax.swing.JTextField();
        btnSeleccionarNC = new javax.swing.JButton();
        jLabel17 = new javax.swing.JLabel();
        txtMontoFinalFinalSoles = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtTipoCambio = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txtFinalDolares = new javax.swing.JTextField();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(784, 600));
        setMinimumSize(new java.awt.Dimension(1, 1));
        setPreferredSize(new java.awt.Dimension(784, 600));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Productos"));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(764, 215));
        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(764, 215));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblProductos);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Información de la Venta"));

        jLabel10.setText("Número de Pedido");

        jLabel4.setText("Fecha del Pedido");

        jLabel5.setText("Número de la Cotización");

        jLabel7.setText("Fecha de la Cotización");

        jLabel1.setText("Razón Social/Nombre");

        jLabel2.setText("Número de Documento");

        btnPedido.setText("Seleccionar Pedido");
        btnPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPedidoActionPerformed(evt);
            }
        });

        jLabel3.setText("Fecha de Emisión");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNumPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNumCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(dpFechaCot, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(dpFechaPed, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNumDoc)
                            .addComponent(dpFechaVenta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnPedido, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaCot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaPed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPedido)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Información del Pago"));

        jLabel14.setText("Tipo");

        rbBoleta.setText("Boleta");

        rbFactura.setText("Factura");

        jLabel8.setText("Moneda");

        rbSoles.setText("Soles");
        rbSoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSolesActionPerformed(evt);
            }
        });

        rbDolares.setText("Dólares");
        rbDolares.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDolaresActionPerformed(evt);
            }
        });

        jLabel11.setText("Porcentaje IGV");

        txtPorcentajeIGV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPorcentajeIGV.setToolTipText("");

        jLabel12.setText("Importe IGV");

        txtImporteIGV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel15.setText("Monto sin IGV");

        txtMontoSinIGV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel13.setText("Monto final soles");

        txtFinalSoles.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtFinalSoles.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel6.setText("Monto N.C.");

        txtMontoNC.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        btnSeleccionarNC.setText("Seleccionar Nota de Crédito");
        btnSeleccionarNC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarNCActionPerformed(evt);
            }
        });

        jLabel17.setText("Monto final soles");

        txtMontoFinalFinalSoles.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtMontoFinalFinalSoles.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel9.setText("Tipo de cambio");

        txtTipoCambio.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel16.setText("Monto final dólares");

        txtFinalDolares.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtFinalDolares.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtFinalDolares.setToolTipText("");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(rbBoleta)
                        .addGap(34, 34, 34)
                        .addComponent(rbFactura)
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel16))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtFinalDolares)
                            .addComponent(txtTipoCambio)
                            .addComponent(txtFinalSoles)
                            .addComponent(txtMontoSinIGV)
                            .addComponent(txtImporteIGV)
                            .addComponent(txtPorcentajeIGV, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rbSoles)
                                .addGap(40, 40, 40)
                                .addComponent(rbDolares)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMontoNC)
                            .addComponent(btnSeleccionarNC, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtMontoFinalFinalSoles, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(rbBoleta)
                    .addComponent(rbFactura))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(rbSoles)
                    .addComponent(rbDolares))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPorcentajeIGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtImporteIGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMontoSinIGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFinalSoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMontoNC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSeleccionarNC)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMontoFinalFinalSoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTipoCambio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFinalDolares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnRegistrar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnRegistrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        if(!txtNumCotizacion.getText().equals("")){
            if(valido()){
                if(stockValido()){
                    double monto = Double.parseDouble(GestionVentas.getInstance().Redondear(pedido.getTotal()-ncb.getMonto())+"");
                    String tipo;
                    String tipoMoneda;
                    if(rbBoleta.isSelected())
                        tipo = "Boleta";
                    else
                        tipo = "Factura";
                    if(rbSoles.isSelected())
                        tipoMoneda = "SOLES";
                    else
                        tipoMoneda = "DOLARES";
                    Double montoDolares = txtFinalDolares.getText().equals("-")?0:GestionVentas.getInstance().Redondear(Double.parseDouble(txtFinalDolares.getText()));

                    Facturacion facturacion = new Facturacion(null,pedido.getIdCliente(),pedido.getIdPedido(),ncb.getIdNotaCredito(),fecha,monto,"Registrado",tipo,igv,tipoMoneda,tipoCambio,montoDolares);
                    registrarSalidas();
                    GestionVentas.getInstance().registrarFacturacion(facturacion,pedido.getIdCotizacion());
                    JOptionPane.showMessageDialog(this,"Se registró correctamente la facturación.");
                    refPanel.refreshItemsTable();
                    parent.dispose(); 
                }
                else {
                    JOptionPane.showMessageDialog(this, "Stock insuficiente para realizar la venta.");
                    btnRegistrar.setEnabled(false);
                    btnRegistrar.setVisible(false);
                }
            }
            else 
                JOptionPane.showMessageDialog(this, "Debe debe seleccionar el tipo de pago.");
        }
        else
            JOptionPane.showMessageDialog(this,"Debe seleccionar un pedido.");
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        parent.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPedidoActionPerformed
        PedidoBuscarJDialog buscarCotDialog = new PedidoBuscarJDialog(null, true);
        buscarCotDialog.setRefPanel(this);
        buscarCotDialog.setVisible(true);
    }//GEN-LAST:event_btnPedidoActionPerformed

    private void rbDolaresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDolaresActionPerformed
        actualizaDolares();
    }//GEN-LAST:event_rbDolaresActionPerformed

    private void rbSolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSolesActionPerformed
        double montofinalsoles = Double.parseDouble(pedido.getTotal()+"");
        txtImporteIGV.setText(df.format(GestionVentas.getInstance().Redondear(0.18*montofinalsoles/1.18)));
        txtMontoSinIGV.setText(df.format(GestionVentas.getInstance().Redondear(montofinalsoles/1.18)));
        txtFinalSoles.setText(df.format(pedido.getTotal()));
        txtTipoCambio.setText("-");
        txtFinalDolares.setText("-");
    }//GEN-LAST:event_rbSolesActionPerformed

    private void btnSeleccionarNCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarNCActionPerformed
        NotaCreditoBuscarJDialog buscarDialog = new NotaCreditoBuscarJDialog(null, true);
        buscarDialog.setNC(this);
        buscarDialog.setCodCliente(pedido.getIdCliente());
        buscarDialog.actualiza();
        buscarDialog.setVisible(true);
    }//GEN-LAST:event_btnSeleccionarNCActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.ButtonGroup btnGpMoneda;
    private javax.swing.ButtonGroup btnGpTipo;
    private javax.swing.JButton btnPedido;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSeleccionarNC;
    private org.jdesktop.swingx.JXDatePicker dpFechaCot;
    private org.jdesktop.swingx.JXDatePicker dpFechaPed;
    private org.jdesktop.swingx.JXDatePicker dpFechaVenta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbBoleta;
    private javax.swing.JRadioButton rbDolares;
    private javax.swing.JRadioButton rbFactura;
    private javax.swing.JRadioButton rbSoles;
    private javax.swing.JTable tblProductos;
    private javax.swing.JTextField txtFinalDolares;
    private javax.swing.JTextField txtFinalSoles;
    private javax.swing.JTextField txtImporteIGV;
    private javax.swing.JTextField txtMontoFinalFinalSoles;
    private javax.swing.JTextField txtMontoNC;
    private javax.swing.JTextField txtMontoSinIGV;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumCotizacion;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtNumPedido;
    private javax.swing.JTextField txtPorcentajeIGV;
    private javax.swing.JTextField txtTipoCambio;
    // End of variables declaration//GEN-END:variables
}
